import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import '../../../services/address.dart';
import '../../../services/dio_manager.dart';
import '../my_project_detail_module/my_project_detail_view.dart';
import '../top_dialog_page.dart';
import 'my_project_model.dart';

class MyProjectController extends GetxController {
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    requestData(project_type: projectType);
  }

  EasyRefreshController easyRefreshController = EasyRefreshController();

  TextEditingController editingController = TextEditingController();

  bool isTopShow = false;

  String replyType = '我管理的';

  var page = 1;
  String projectType = 'manager';

  var dataArr = [];

  /// 获取我的项目列表数据
  /// 筛选条件manager-我管理的，create-我创建的，collaborate-我参与的，
  /// about-我负责和参与的，focus-我关注的,closed-已关闭的
  requestData({var project_type, var search,}) async {
    var params = {
      'project_type': project_type,
      'page': page,
      'limit': '10',
    };
    var json = await DioManager()
        .kkRequest(Address.projectList, params: params, method: 'get');

    MyProjectModel _model = MyProjectModel.fromJson(json);

    if (_model.data == null) {
      // dataArr.clear();
      print('暂无更多');
      easyRefreshController.finishLoad(noMore: true);
      // BotToast.showText(text: '暂无更多');
      update();
      // return;
    }

    if (page == 1) {
      easyRefreshController.resetLoadState();
      dataArr.clear();
      if(_model.data!=null){
        dataArr.addAll(_model.data!);
      }
    } else {
      if(_model.data!=null){
        dataArr.addAll(_model.data!);
      }
      if (_model.data == null) {
        easyRefreshController.finishLoad(noMore: true);
        BotToast.showText(text: '暂无更多');
      }
    }
    update();
  }

  /// 点击的弹框
  topClick() async {
    isTopShow = true;

    var a;
    if (replyType == '我管理的') {
      a = 0;
    }
    if (replyType == '我创建的') {
      a = 1;
    }
    if (replyType == '我负责的') {
      a = 2;
    }
    if (replyType == '我参与的项目') {
      a = 3;
    }

    if (replyType == '我关注的') {
      a = 4;
    }
    if (replyType == '已关闭的') {
      a = 5;
    }

    var data =
        await Get.dialog(TopDialogPage(a), barrierColor: Colors.transparent);
    if (data != null) {
      print(data);
      isTopShow = data['isShow'];
      replyType = data['text'];
      projectType = data['reply_type'];
      page = 1;
      requestData(project_type: projectType);
    }
    update();
  }

  ///下拉刷新
  pullToData() async {
    page = 1;
    requestData(project_type: projectType);
  }

  /// 上拉加载
  onLoadToData() async {
    page++;
    requestData(project_type: projectType);
  }

  /// 跳转项目详情页面;
  ///
  pushToView(int index) async {
    MyProjectItemModel model = dataArr[index];

    Get.to(ProjectManagerView(),arguments: model.id);

    // var data = await Get.toNamed('/myProjectDetailDetail', arguments: model.id);
    // if (data == 'refresh') {
    //   requestData(project_type: projectType);
    // }
  }
}
